/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.upf.grupE.interftest;

import edu.upf.grupE.Entities.Picture;
import edu.upf.grupE.Entities.Video;
import edu.upf.grupE.Video.AVReceive2;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

/**
 *
 * @author Madhava Carrillo <madhava at me.com>
 */
public class EventListenerImpl implements GUIEventListener{
    MainPage page;
    Login login;
    Search search;
    Buddies buddies;
    ImageSelection im;
    VideoSelection vid;

    public EventListenerImpl(MainPage page, Login login, Search search, Buddies buddies, ImageSelection im, VideoSelection vid){
        this.page = page;
        this.login=login;
        this.search=search;
        this.buddies=buddies;
        this.im=im;
        this.vid=vid;
    }
    @Override
    public void recivedMessage(String text, String email) {
    
        buddies.findUser(email, text);
    }

    @Override
    public void recivedResult(Picture pic, String from) {
     search.PrintResults(pic, from);
    }

    @Override
    public void refreshListPicture() {
       ///Important aquesta!!!
        im.UpdateAlbums();
    }

    @Override
    public void refreshListVideo() {
        vid.UpdateAlbums();

    }

    @Override
    public void recivedPicture(Picture pic, ImageIcon icon) {
        //Podries fer k aki la llsita de downloading poses downloaded? I que en comptes del 0% kuan et baixes una poses downloading?

    }

    @Override
    public void setOnline() {
        
        login.setLogin(true);

    }


    public void showMessage(String msg){
       JOptionPane.showMessageDialog(page.log, msg, "Error", JOptionPane.ERROR_MESSAGE);
    }

    public void setLoginMsg(String message){
        page.log.lbl_message.setText("Error conectando al servidor. Revisa tu user/pass");
    }

    @Override
    public void setOffline() {

        page.setLogout();
    }

    @Override
    public void startVideoRep(String ip, int port) {
    String[] rec=new String[2];
    rec[0]= ip + "/" + port;
    rec[1]= ip + "/" + (port+2);

    // Creating the receiver object.
	AVReceive2 avReceive = new AVReceive2(rec);
   
    }

    @Override
    public void actualizeUserList() {
        try {
            Thread.sleep((Math.round(Math.random() * 1000)));
        } catch (InterruptedException ex) {
            Logger.getLogger(EventListenerImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        buddies.updateUsers();
        search.UpdateUsers();
    }

        @Override
    public void recivedVideoResult(Video vid, String from) {
     //Oscar, akesta!!!
            search.PrintResults(vid, from);

    }

}
